{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f88a922",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------------\n",
      "Structure of the actions for Gaugefields\n",
      "num. of terms: 1\n",
      "-------------------------------\n",
      "      1-st term: \n",
      "          coefficient: 2.85\n",
      "      -------------------------\n",
      "1-st loop\n",
      "L\"$U_{1}(n)U_{2}(n+e_{1})U^{\\dagger}_{1}(n+e_{2})U^{\\dagger}_{2}(n)$\"\t\n",
      "2-nd loop\n",
      "L\"$U_{1}(n)U_{3}(n+e_{1})U^{\\dagger}_{1}(n+e_{3})U^{\\dagger}_{3}(n)$\"\t\n",
      "3-rd loop\n",
      "L\"$U_{1}(n)U_{4}(n+e_{1})U^{\\dagger}_{1}(n+e_{4})U^{\\dagger}_{4}(n)$\"\t\n",
      "4-th loop\n",
      "L\"$U_{2}(n)U_{3}(n+e_{2})U^{\\dagger}_{2}(n+e_{3})U^{\\dagger}_{3}(n)$\"\t\n",
      "5-th loop\n",
      "L\"$U_{2}(n)U_{4}(n+e_{2})U^{\\dagger}_{2}(n+e_{4})U^{\\dagger}_{4}(n)$\"\t\n",
      "6-th loop\n",
      "L\"$U_{3}(n)U_{4}(n+e_{3})U^{\\dagger}_{3}(n+e_{4})U^{\\dagger}_{4}(n)$\"\t\n",
      "7-th loop\n",
      "L\"$U_{2}(n)U_{1}(n+e_{2})U^{\\dagger}_{2}(n+e_{1})U^{\\dagger}_{1}(n)$\"\t\n",
      "8-th loop\n",
      "L\"$U_{3}(n)U_{1}(n+e_{3})U^{\\dagger}_{3}(n+e_{1})U^{\\dagger}_{1}(n)$\"\t\n",
      "9-th loop\n",
      "L\"$U_{4}(n)U_{1}(n+e_{4})U^{\\dagger}_{4}(n+e_{1})U^{\\dagger}_{1}(n)$\"\t\n",
      "10-th loop\n",
      "L\"$U_{3}(n)U_{2}(n+e_{3})U^{\\dagger}_{3}(n+e_{2})U^{\\dagger}_{2}(n)$\"\t\n",
      "11-th loop\n",
      "L\"$U_{4}(n)U_{2}(n+e_{4})U^{\\dagger}_{4}(n+e_{2})U^{\\dagger}_{2}(n)$\"\t\n",
      "12-th loop\n",
      "L\"$U_{4}(n)U_{3}(n+e_{4})U^{\\dagger}_{4}(n+e_{3})U^{\\dagger}_{3}(n)$\"\t\n",
      "      -------------------------\n",
      "----------------------------------------------\n",
      "Sold = -4644.156266093941, Snew = -4644.3632567032\n",
      "Snew - Sold = -0.2069906092592646\n",
      "1 plaq_t = 0.8238060637427356\n",
      "acceptance ratio 1.0\n",
      "Sold = -3131.6988458247406, Snew = -3131.8139244197714\n",
      "Snew - Sold = -0.11507859503080908\n",
      "2 plaq_t = 0.7255851804812975\n",
      "acceptance ratio 1.0\n",
      "Sold = -2218.0357556821764, Snew = -2218.0946210019047\n",
      "Snew - Sold = -0.058865319728283794\n",
      "3 plaq_t = 0.7255851804812975\n",
      "acceptance ratio 0.6666666666666666\n",
      "Sold = -2271.910560459489, Snew = -2271.971951413962\n",
      "Snew - Sold = -0.061390954473154125\n",
      "4 plaq_t = 0.6658726030446883\n",
      "acceptance ratio 0.75\n",
      "Sold = -1732.7272539336655, Snew = -1732.7606174867828\n",
      "Snew - Sold = -0.033363553117396805\n",
      "5 plaq_t = 0.6353689534829944\n",
      "acceptance ratio 0.8\n",
      "Sold = -1469.2449713159622, Snew = -1469.2621847311148\n",
      "Snew - Sold = -0.017213415152582456\n",
      "6 plaq_t = 0.6225340851293024\n",
      "acceptance ratio 0.8333333333333334\n",
      "Sold = -1307.831896237526, Snew = -1307.8430813581535\n",
      "Snew - Sold = -0.011185120627487777\n",
      "7 plaq_t = 0.5926264058468408\n",
      "acceptance ratio 0.8571428571428571\n",
      "Sold = -1020.6802342326973, Snew = -1020.6895149972261\n",
      "Snew - Sold = -0.009280764528739383\n",
      "8 plaq_t = 0.5801531851076368\n",
      "acceptance ratio 0.875\n",
      "Sold = -869.3869597394787, Snew = -869.393853263131\n",
      "Snew - Sold = -0.006893523652252043\n",
      "9 plaq_t = 0.5625508975424091\n",
      "acceptance ratio 0.8888888888888888\n",
      "Sold = -797.8650876451857, Snew = -797.8582248483535\n",
      "Snew - Sold = 0.006862796832137974\n",
      "10 plaq_t = 0.5695065942575936\n",
      "acceptance ratio 0.9\n",
      "Sold = -922.0729581863375, Snew = -922.0775291522514\n",
      "Snew - Sold = -0.004570965913899272\n",
      "11 plaq_t = 0.5725047126534745\n",
      "acceptance ratio 0.9090909090909091\n",
      "Sold = -902.4592573471618, Snew = -902.4595115776065\n",
      "Snew - Sold = -0.00025423044462513644\n",
      "12 plaq_t = 0.5710403680789145\n",
      "acceptance ratio 0.9166666666666666\n",
      "Sold = -959.7126552542372, Snew = -959.7099898636043\n",
      "Snew - Sold = 0.0026653906329556776\n",
      "13 plaq_t = 0.5723934478864743\n",
      "acceptance ratio 0.9230769230769231\n",
      "Sold = -980.8527606750267, Snew = -980.8548050409036\n",
      "Snew - Sold = -0.0020443658768272144\n",
      "14 plaq_t = 0.5719707390387985\n",
      "acceptance ratio 0.9285714285714286\n",
      "Sold = -899.0939362686859, Snew = -899.095540401494\n",
      "Snew - Sold = -0.0016041328080973472\n",
      "15 plaq_t = 0.5739260001173908\n",
      "acceptance ratio 0.9333333333333333\n",
      "Sold = -871.9439393865787, Snew = -871.9477327701638\n",
      "Snew - Sold = -0.0037933835851617914\n",
      "16 plaq_t = 0.5650548556088396\n",
      "acceptance ratio 0.9375\n",
      "Sold = -758.4170803513261, Snew = -758.4181345694351\n",
      "Snew - Sold = -0.0010542181089476799\n",
      "17 plaq_t = 0.568401474718363\n",
      "acceptance ratio 0.9411764705882353\n",
      "Sold = -854.1784466886684, Snew = -854.1841843867296\n",
      "Snew - Sold = -0.005737698061238916\n",
      "18 plaq_t = 0.55842865210424\n",
      "acceptance ratio 0.9444444444444444\n",
      "Sold = -753.5630988504436, Snew = -753.5600256222597\n",
      "Snew - Sold = 0.0030732281838936615\n",
      "19 plaq_t = 0.55409681500659\n",
      "acceptance ratio 0.9473684210526315\n",
      "Sold = -811.3993465720305, Snew = -811.3989983050742\n",
      "Snew - Sold = 0.0003482669562799856\n",
      "20 plaq_t = 0.5724154413083474\n",
      "acceptance ratio 0.95\n",
      "Sold = -923.1811096164447, Snew = -923.1885476922112\n",
      "Snew - Sold = -0.007438075766458496\n",
      "21 plaq_t = 0.5646825905099592\n",
      "acceptance ratio 0.9523809523809523\n",
      "Sold = -819.0867041810825, Snew = -819.0815530730206\n",
      "Snew - Sold = 0.005151108061909326\n",
      "22 plaq_t = 0.5689583248494593\n",
      "acceptance ratio 0.9545454545454546\n",
      "Sold = -862.0352480372676, Snew = -862.0331853256866\n",
      "Snew - Sold = 0.002062711580947507\n",
      "23 plaq_t = 0.5631408749055541\n",
      "acceptance ratio 0.9565217391304348\n",
      "Sold = -825.4180418940523, Snew = -825.4199752211862\n",
      "Snew - Sold = -0.0019333271338837221\n",
      "24 plaq_t = 0.5585069788778944\n",
      "acceptance ratio 0.9583333333333334\n",
      "Sold = -747.1253709356697, Snew = -747.1204533598584\n",
      "Snew - Sold = 0.0049175758113051415\n",
      "25 plaq_t = 0.5561451351104562\n",
      "acceptance ratio 0.96\n",
      "Sold = -772.6220225698535, Snew = -772.6288504212844\n",
      "Snew - Sold = -0.0068278514308985905\n",
      "26 plaq_t = 0.5514744693762469\n",
      "acceptance ratio 0.9615384615384616\n",
      "Sold = -728.876328890562, Snew = -728.8693397360703\n",
      "Snew - Sold = 0.006989154491748195\n",
      "27 plaq_t = 0.5586608019538342\n",
      "acceptance ratio 0.9629629629629629\n",
      "Sold = -824.9633238739534, Snew = -824.9579843921347\n",
      "Snew - Sold = 0.005339481818737113\n",
      "28 plaq_t = 0.55739967860662\n",
      "acceptance ratio 0.9642857142857143\n",
      "Sold = -818.5161451611411, Snew = -818.5191434862318\n",
      "Snew - Sold = -0.0029983250906298053\n",
      "29 plaq_t = 0.554101379847307\n",
      "acceptance ratio 0.9655172413793104\n",
      "Sold = -750.4773112715184, Snew = -750.4790174417712\n",
      "Snew - Sold = -0.0017061702528735623\n",
      "30 plaq_t = 0.5538106063375524\n",
      "acceptance ratio 0.9666666666666667\n",
      "Sold = -668.2560386647301, Snew = -668.2545412846794\n",
      "Snew - Sold = 0.0014973800507505075\n",
      "31 plaq_t = 0.5471198181748804\n",
      "acceptance ratio 0.967741935483871\n",
      "Sold = -738.192730049027, Snew = -738.194813249168\n",
      "Snew - Sold = -0.0020832001409871737\n",
      "32 plaq_t = 0.5530180194187104\n",
      "acceptance ratio 0.96875\n",
      "Sold = -783.7249695327782, Snew = -783.7203209378049\n",
      "Snew - Sold = 0.004648594973332365\n",
      "33 plaq_t = 0.5587120038281465\n",
      "acceptance ratio 0.9696969696969697\n",
      "Sold = -851.1856528017915, Snew = -851.1814679548797\n",
      "Snew - Sold = 0.004184846911812201\n",
      "34 plaq_t = 0.5667815151566749\n",
      "acceptance ratio 0.9705882352941176\n",
      "Sold = -897.7552377895158, Snew = -897.7566953860764\n",
      "Snew - Sold = -0.0014575965606127284\n",
      "35 plaq_t = 0.5609241355706241\n",
      "acceptance ratio 0.9714285714285714\n",
      "Sold = -840.3584899994803, Snew = -840.3590643298435\n",
      "Snew - Sold = -0.0005743303631788876\n",
      "36 plaq_t = 0.5597855460838441\n",
      "acceptance ratio 0.9722222222222222\n",
      "Sold = -879.504736231388, Snew = -879.5105697468862\n",
      "Snew - Sold = -0.0058335154981250525\n",
      "37 plaq_t = 0.5603617346862434\n",
      "acceptance ratio 0.972972972972973\n",
      "Sold = -820.0418080871223, Snew = -820.0439176606646\n",
      "Snew - Sold = -0.002109573542384169\n",
      "38 plaq_t = 0.5586660927490323\n",
      "acceptance ratio 0.9736842105263158\n",
      "Sold = -719.5077517989475, Snew = -719.5033291364871\n",
      "Snew - Sold = 0.004422662460456195\n",
      "39 plaq_t = 0.5514920663425594\n",
      "acceptance ratio 0.9743589743589743\n",
      "Sold = -668.9074285206561, Snew = -668.908055476817\n",
      "Snew - Sold = -0.0006269561608860386\n",
      "40 plaq_t = 0.554927814411275\n",
      "acceptance ratio 0.975\n",
      "Sold = -734.8726952163288, Snew = -734.8744250868967\n",
      "Snew - Sold = -0.001729870567942271\n",
      "41 plaq_t = 0.5485394052256479\n",
      "acceptance ratio 0.975609756097561\n",
      "Sold = -641.4465764961415, Snew = -641.4467200133959\n",
      "Snew - Sold = -0.0001435172544006491\n",
      "42 plaq_t = 0.546713572147008\n",
      "acceptance ratio 0.9761904761904762\n",
      "Sold = -641.3381796555805, Snew = -641.344199159289\n",
      "Snew - Sold = -0.00601950370855775\n",
      "43 plaq_t = 0.5439471127297664\n",
      "acceptance ratio 0.9767441860465116\n",
      "Sold = -539.3566514411259, Snew = -539.3549451563531\n",
      "Snew - Sold = 0.0017062847728084307\n",
      "44 plaq_t = 0.5406294646830363\n",
      "acceptance ratio 0.9772727272727273\n",
      "Sold = -756.8808440442867, Snew = -756.8761207566699\n",
      "Snew - Sold = 0.0047232876167981885\n",
      "45 plaq_t = 0.5508219045747914\n",
      "acceptance ratio 0.9777777777777777\n",
      "Sold = -801.2471248834818, Snew = -801.2468434973771\n",
      "Snew - Sold = 0.00028138610468886327\n",
      "46 plaq_t = 0.5544350201570106\n",
      "acceptance ratio 0.9782608695652174\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sold = -656.830778985176, Snew = -656.8364612482237\n",
      "Snew - Sold = -0.005682263047674496\n",
      "47 plaq_t = 0.5485216985475407\n",
      "acceptance ratio 0.9787234042553191\n",
      "Sold = -612.6616995545965, Snew = -612.6643971179337\n",
      "Snew - Sold = -0.0026975633372785524\n",
      "48 plaq_t = 0.5525582498544662\n",
      "acceptance ratio 0.9791666666666666\n",
      "Sold = -659.9712282289684, Snew = -659.9638146705383\n",
      "Snew - Sold = 0.007413558430016565\n",
      "49 plaq_t = 0.5547377404304787\n",
      "acceptance ratio 0.9795918367346939\n",
      "Sold = -718.0826540222888, Snew = -718.0940101482438\n",
      "Snew - Sold = -0.011356125954989693\n",
      "50 plaq_t = 0.5486883948867755\n",
      "acceptance ratio 0.98\n",
      "Sold = -756.9994404721956, Snew = -756.9977413339329\n",
      "Snew - Sold = 0.001699138262665656\n",
      "51 plaq_t = 0.5588852100784051\n",
      "acceptance ratio 0.9803921568627451\n",
      "Sold = -815.7913812063798, Snew = -815.7843193843128\n",
      "Snew - Sold = 0.007061822067043977\n",
      "52 plaq_t = 0.5671401244421646\n",
      "acceptance ratio 0.9807692307692307\n",
      "Sold = -895.749752898676, Snew = -895.7483565138455\n",
      "Snew - Sold = 0.0013963848305138526\n",
      "53 plaq_t = 0.5764023004942762\n",
      "acceptance ratio 0.9811320754716981\n",
      "Sold = -1033.3116742238717, Snew = -1033.3163163059044\n",
      "Snew - Sold = -0.004642082032660255\n",
      "54 plaq_t = 0.571529123602827\n",
      "acceptance ratio 0.9814814814814815\n",
      "Sold = -914.7550799842347, Snew = -914.7554831311663\n",
      "Snew - Sold = -0.00040314693160325987\n",
      "55 plaq_t = 0.5683042666131676\n",
      "acceptance ratio 0.9818181818181818\n",
      "Sold = -852.2052267852487, Snew = -852.2094732643482\n",
      "Snew - Sold = -0.004246479099492717\n",
      "56 plaq_t = 0.5682425448036968\n",
      "acceptance ratio 0.9821428571428571\n",
      "Sold = -916.870797517563, Snew = -916.8643453454715\n",
      "Snew - Sold = 0.006452172091485409\n",
      "57 plaq_t = 0.5712086779454674\n",
      "acceptance ratio 0.9824561403508771\n",
      "Sold = -1067.954579829091, Snew = -1067.948212711306\n",
      "Snew - Sold = 0.006367117784975562\n",
      "58 plaq_t = 0.5864536034350043\n",
      "acceptance ratio 0.9827586206896551\n",
      "Sold = -1006.8511615728439, Snew = -1006.8476797288477\n",
      "Snew - Sold = 0.0034818439962691627\n",
      "59 plaq_t = 0.5721230219119979\n",
      "acceptance ratio 0.9830508474576272\n",
      "Sold = -898.2811281219956, Snew = -898.2950444524313\n",
      "Snew - Sold = -0.013916330435677082\n",
      "60 plaq_t = 0.5605950808064741\n",
      "acceptance ratio 0.9833333333333333\n",
      "Sold = -782.9962389432967, Snew = -783.0000479404389\n",
      "Snew - Sold = -0.003808997142186854\n",
      "61 plaq_t = 0.5532290962749132\n",
      "acceptance ratio 0.9836065573770492\n",
      "Sold = -682.5866857244164, Snew = -682.5888173448511\n",
      "Snew - Sold = -0.002131620434738579\n",
      "62 plaq_t = 0.5572733739635573\n",
      "acceptance ratio 0.9838709677419355\n",
      "Sold = -780.2975848502065, Snew = -780.2990539142038\n",
      "Snew - Sold = -0.0014690639973196085\n",
      "63 plaq_t = 0.5549215380342337\n",
      "acceptance ratio 0.9841269841269841\n",
      "Sold = -870.7475808070863, Snew = -870.7380436463272\n",
      "Snew - Sold = 0.009537160759009566\n",
      "64 plaq_t = 0.5728528499110478\n",
      "acceptance ratio 0.984375\n",
      "Sold = -846.579907710764, Snew = -846.58114358489\n",
      "Snew - Sold = -0.001235874125995906\n",
      "65 plaq_t = 0.5630205812659633\n",
      "acceptance ratio 0.9846153846153847\n",
      "Sold = -776.0836070455834, Snew = -776.0838782011087\n",
      "Snew - Sold = -0.00027115552529721754\n",
      "66 plaq_t = 0.5567068334650107\n",
      "acceptance ratio 0.9848484848484849\n",
      "Sold = -729.0760119711967, Snew = -729.0679424728396\n",
      "Snew - Sold = 0.008069498357144766\n",
      "67 plaq_t = 0.5657503107762185\n",
      "acceptance ratio 0.9850746268656716\n",
      "Sold = -729.8088851342227, Snew = -729.8162575339229\n",
      "Snew - Sold = -0.007372399700216192\n",
      "68 plaq_t = 0.5512651485085336\n",
      "acceptance ratio 0.9852941176470589\n",
      "Sold = -743.2307748951462, Snew = -743.2213498068968\n",
      "Snew - Sold = 0.009425088249372493\n",
      "69 plaq_t = 0.5530711513069271\n",
      "acceptance ratio 0.9855072463768116\n",
      "Sold = -699.7372551274266, Snew = -699.7339290108848\n",
      "Snew - Sold = 0.0033261165417570737\n",
      "70 plaq_t = 0.5585730160745582\n",
      "acceptance ratio 0.9857142857142858\n",
      "Sold = -775.1769010735861, Snew = -775.1795570370195\n",
      "Snew - Sold = -0.0026559634334262228\n",
      "71 plaq_t = 0.5525874658569706\n",
      "acceptance ratio 0.9859154929577465\n",
      "Sold = -732.0262054931163, Snew = -732.0222205221894\n",
      "Snew - Sold = 0.00398497092692196\n",
      "72 plaq_t = 0.5497818770682583\n",
      "acceptance ratio 0.9861111111111112\n",
      "Sold = -745.7792235650086, Snew = -745.7747903419659\n",
      "Snew - Sold = 0.004433223042724421\n",
      "73 plaq_t = 0.558917099727103\n",
      "acceptance ratio 0.9863013698630136\n",
      "Sold = -719.7496151364612, Snew = -719.7544459523187\n",
      "Snew - Sold = -0.004830815857530979\n",
      "74 plaq_t = 0.5560090493956209\n",
      "acceptance ratio 0.9864864864864865\n",
      "Sold = -777.8519342767149, Snew = -777.8498148550143\n",
      "Snew - Sold = 0.0021194217006268445\n",
      "75 plaq_t = 0.5577188299961345\n",
      "acceptance ratio 0.9866666666666667\n",
      "Sold = -864.5898103976901, Snew = -864.5959977536208\n",
      "Snew - Sold = -0.006187355930705962\n",
      "76 plaq_t = 0.5585053698934693\n",
      "acceptance ratio 0.9868421052631579\n",
      "Sold = -749.6023707879749, Snew = -749.6069348225492\n",
      "Snew - Sold = -0.004564034574286779\n",
      "77 plaq_t = 0.5441796560226624\n",
      "acceptance ratio 0.987012987012987\n",
      "Sold = -680.9482245600016, Snew = -680.9387517276773\n",
      "Snew - Sold = 0.009472832324263436\n",
      "78 plaq_t = 0.5507216093550326\n",
      "acceptance ratio 0.9871794871794872\n",
      "Sold = -727.880190800654, Snew = -727.887946084199\n",
      "Snew - Sold = -0.0077552835450660496\n",
      "79 plaq_t = 0.5496960352638441\n",
      "acceptance ratio 0.9873417721518988\n",
      "Sold = -691.9269444309712, Snew = -691.9363410009646\n",
      "Snew - Sold = -0.009396569993441517\n",
      "80 plaq_t = 0.5384122356883635\n",
      "acceptance ratio 0.9875\n",
      "Sold = -559.491907094618, Snew = -559.4972611175381\n",
      "Snew - Sold = -0.005354022920073476\n",
      "81 plaq_t = 0.5418057307179261\n",
      "acceptance ratio 0.9876543209876543\n",
      "Sold = -521.0311294675394, Snew = -521.0291243744778\n",
      "Snew - Sold = 0.002005093061598018\n",
      "82 plaq_t = 0.5321263589598276\n",
      "acceptance ratio 0.9878048780487805\n",
      "Sold = -482.00641891956457, Snew = -482.00967439717533\n",
      "Snew - Sold = -0.003255477610764501\n",
      "83 plaq_t = 0.5316663767101343\n",
      "acceptance ratio 0.9879518072289156\n",
      "Sold = -597.1034694714835, Snew = -597.0885035883921\n",
      "Snew - Sold = 0.014965883091463184\n",
      "84 plaq_t = 0.5613617797246478\n",
      "acceptance ratio 0.9880952380952381\n",
      "Sold = -847.0975277844241, Snew = -847.0933306390089\n",
      "Snew - Sold = 0.0041971454152189835\n",
      "85 plaq_t = 0.5697632580393801\n",
      "acceptance ratio 0.9882352941176471\n",
      "Sold = -897.1116780533312, Snew = -897.1108185486728\n",
      "Snew - Sold = 0.0008595046583650401\n",
      "86 plaq_t = 0.5686704596439502\n",
      "acceptance ratio 0.9883720930232558\n",
      "Sold = -801.4343599382246, Snew = -801.4436153788374\n",
      "Snew - Sold = -0.0092554406128329\n",
      "87 plaq_t = 0.5596206638430906\n",
      "acceptance ratio 0.9885057471264368\n",
      "Sold = -715.5182805666582, Snew = -715.5224324987039\n",
      "Snew - Sold = -0.004151932045715512\n",
      "88 plaq_t = 0.5557130671723673\n",
      "acceptance ratio 0.9886363636363636\n",
      "Sold = -685.5772476162992, Snew = -685.5762809892985\n",
      "Snew - Sold = 0.0009666270007073763\n",
      "89 plaq_t = 0.5436065891091535\n",
      "acceptance ratio 0.9887640449438202\n",
      "Sold = -523.7440524029844, Snew = -523.7468646609632\n",
      "Snew - Sold = -0.0028122579788032454\n",
      "90 plaq_t = 0.5309824407526063\n",
      "acceptance ratio 0.9888888888888889\n",
      "Sold = -564.8219970379423, Snew = -564.8142570503032\n",
      "Snew - Sold = 0.007739987639070023\n",
      "91 plaq_t = 0.556309231568429\n",
      "acceptance ratio 0.989010989010989\n",
      "Sold = -754.7782849093501, Snew = -754.7802284570153\n",
      "Snew - Sold = -0.0019435476651779027\n",
      "92 plaq_t = 0.5574068225369021\n",
      "acceptance ratio 0.9891304347826086\n",
      "Sold = -825.3827211660719, Snew = -825.3722834323426\n",
      "Snew - Sold = 0.010437733729304455\n",
      "93 plaq_t = 0.5680990451090989\n",
      "acceptance ratio 0.989247311827957\n",
      "Sold = -845.6639173876583, Snew = -845.6658796791689\n",
      "Snew - Sold = -0.0019622915106083383\n",
      "94 plaq_t = 0.5719176999422794\n",
      "acceptance ratio 0.9893617021276596\n",
      "Sold = -935.9397438360947, Snew = -935.9468889809259\n",
      "Snew - Sold = -0.007145144831156358\n",
      "95 plaq_t = 0.5687889233104195\n",
      "acceptance ratio 0.9894736842105263\n",
      "Sold = -871.0674305381326, Snew = -871.0698259656301\n",
      "Snew - Sold = -0.002395427497503988\n",
      "96 plaq_t = 0.5641752049272305\n",
      "acceptance ratio 0.9895833333333334\n",
      "Sold = -827.8068897766798, Snew = -827.8049512744883\n",
      "Snew - Sold = 0.0019385021914786194\n",
      "97 plaq_t = 0.5657431774935067\n",
      "acceptance ratio 0.9896907216494846\n",
      "Sold = -949.4959559761437, Snew = -949.4972523649831"
     ]
    }
   ],
   "source": [
    "using Gaugefields\n",
    "using LinearAlgebra\n",
    "using LaTeXStrings\n",
    "\n",
    "function MDtest!(gauge_action,U,Dim)\n",
    "    p = initialize_TA_Gaugefields(U) #This is a traceless-antihermitian gauge fields. This has NC^2-1 real coefficients. \n",
    "    Uold = similar(U)\n",
    "    substitute_U!(Uold,U)\n",
    "    MDsteps = 100\n",
    "    temp1 = similar(U[1])\n",
    "    temp2 = similar(U[1])\n",
    "    comb = 6\n",
    "    factor = 1/(comb*U[1].NV*U[1].NC)\n",
    "    numaccepted = 0\n",
    "\n",
    "    numtrj = 100\n",
    "    for itrj = 1:numtrj\n",
    "        accepted = MDstep!(gauge_action,U,p,MDsteps,Dim,Uold)\n",
    "        numaccepted += ifelse(accepted,1,0)\n",
    "\n",
    "        plaq_t = calculate_Plaquette(U,temp1,temp2)*factor\n",
    "        println(\"$itrj plaq_t = $plaq_t\")\n",
    "        println(\"acceptance ratio \",numaccepted/itrj)\n",
    "    end\n",
    "end\n",
    "\n",
    "function calc_action(gauge_action,U,p)\n",
    "    NC = U[1].NC\n",
    "    Sg = -evaluate_GaugeAction(gauge_action,U)/NC #evaluate_GaugeAction(gauge_action,U) = tr(evaluate_GaugeAction_untraced(gauge_action,U))\n",
    "    Sp = p*p/2\n",
    "    S = Sp + Sg\n",
    "    return real(S)\n",
    "end\n",
    "\n",
    "\n",
    "function MDstep!(gauge_action,U,p,MDsteps,Dim,Uold)\n",
    "    Δτ = 1/MDsteps\n",
    "    gauss_distribution!(p)\n",
    "    Sold = calc_action(gauge_action,U,p)\n",
    "    substitute_U!(Uold,U)\n",
    "\n",
    "    for itrj=1:MDsteps\n",
    "        U_update!(U,p,0.5,Δτ,Dim,gauge_action)\n",
    "\n",
    "        P_update!(U,p,1.0,Δτ,Dim,gauge_action)\n",
    "\n",
    "        U_update!(U,p,0.5,Δτ,Dim,gauge_action)\n",
    "    end\n",
    "    Snew = calc_action(gauge_action,U,p)\n",
    "    println(\"Sold = $Sold, Snew = $Snew\")\n",
    "    println(\"Snew - Sold = $(Snew-Sold)\")\n",
    "    ratio = min(1,exp(Snew-Sold))\n",
    "    if rand() > ratio\n",
    "        substitute_U!(U,Uold)\n",
    "        return false\n",
    "    else\n",
    "        return true\n",
    "    end\n",
    "end\n",
    "\n",
    "function U_update!(U,p,ϵ,Δτ,Dim,gauge_action)\n",
    "    temps = get_temporary_gaugefields(gauge_action)\n",
    "    temp1 = temps[1]\n",
    "    temp2 = temps[2]\n",
    "    expU = temps[3]\n",
    "    W = temps[4]\n",
    "\n",
    "    for μ=1:Dim\n",
    "        exptU!(expU,ϵ*Δτ,p[μ],[temp1,temp2])\n",
    "        mul!(W,expU,U[μ])\n",
    "        substitute_U!(U[μ],W)\n",
    "        \n",
    "    end\n",
    "end\n",
    "\n",
    "function P_update!(U,p,ϵ,Δτ,Dim,gauge_action) # p -> p +factor*U*dSdUμ\n",
    "    NC = U[1].NC\n",
    "    temps = get_temporary_gaugefields(gauge_action)\n",
    "    dSdUμ = temps[end]\n",
    "    factor =  -ϵ*Δτ/(NC)\n",
    "\n",
    "    for μ=1:Dim\n",
    "        calc_dSdUμ!(dSdUμ,gauge_action,μ,U)\n",
    "        mul!(temps[1],U[μ],dSdUμ) # U*dSdUμ\n",
    "        Traceless_antihermitian_add!(p[μ],factor,temps[1])\n",
    "    end\n",
    "end\n",
    "\n",
    "function test1()\n",
    "    NX = 4\n",
    "    NY = 4\n",
    "    NZ = 4\n",
    "    NT = 4\n",
    "    Nwing = 1\n",
    "    Dim = 4\n",
    "    NC = 3\n",
    "\n",
    "    U  =Initialize_Gaugefields(NC,Nwing,NX,NY,NZ,NT,condition = \"cold\")\n",
    "\n",
    "\n",
    "    gauge_action = GaugeAction(U)\n",
    "    plaqloop = make_loops_fromname(\"plaquette\")\n",
    "    append!(plaqloop,plaqloop')\n",
    "    β = 5.7/2\n",
    "    push!(gauge_action,β,plaqloop)\n",
    "    \n",
    "    show(gauge_action)\n",
    "\n",
    "    MDtest!(gauge_action,U,Dim)\n",
    "\n",
    "end\n",
    "\n",
    "\n",
    "test1()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.6.2",
   "language": "julia",
   "name": "julia-1.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
